package account

import (
	"cowb/gateway"
	"cowb/log"
	"cowb/rpcpool"
)

type Account struct {
}

func (this *Account) HandleMsg(args []interface{}, reply *[]byte) error {
	ret, err := router.Handle(args[0].(string), args[1].([]byte))
	*reply = ret
	return err
}

func Run(rpcHost string) {
	go func() {
		for {
			gw := rpcpool.Get(":8301")
			if gw == nil {
				log.Error("get :8301 gateway failed! try again")
			} else {
				var reply []byte
				err := gw.Call("Gateway.Regist", &gateway.RegistMsg{ServiceName: "Account", Host: rpcHost, Cmds: router.GetCmds()}, &reply)
				if err != nil {
					log.Error("%v", err)
				} else {
					log.Info("account regist gateway:%s", string(reply))
				}
				return
			}
		}
	}()

	rpcpool.Listen(rpcHost, new(Account))
}
